package com.hspedu.generic;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @Author Stringzhua
 * @Date 2024/3/14 12:47
 * description:
 */
@SuppressWarnings("all")
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee02> employees02 = new ArrayList<>();
        employees02.add(new Employee02("tom", 20000, new Mydate02(1980, 12, 11)));//test4
        employees02.add(new Employee02("jack", 12000, new Mydate02(2001, 12, 12)));
//        empolyees.add(new Employee("hsp", 50000, new MyDate(1980, 10, 10)));//test1
//        empolyees.add(new Employee("tom", 50000, new MyDate(1980, 10, 10)));//test2
        employees02.add(new Employee02("tom", 50000, new Mydate02(1980, 12, 10)));

        System.out.println("empolyees02 = " + employees02);
        System.out.println("=====对雇员进行排序=====");
        //sort方法
        employees02.sort(new Comparator<Employee02>() {
            @Override
            public int compare(Employee02 emp1, Employee02 emp2) {
                //先按照 name 排序，如果 name 相同，则按生日日期的先后排序。
                //先对传入的实参进行验证是否为空
                if (!(emp1 instanceof Employee02 && emp2 instanceof Employee02)) {
                    System.out.println("类型不正确");
                    return 0;
                }
                //比较name
                int i = emp1.getName().compareTo(emp2.getName());
                if (i != 0) {
                    return i;
                }
                //下面是对birthday的比较,因此,我们最好把这个比较放在Mydate类完成
                return emp1.getBirthday().compareTo(emp2.getBirthday());
            }
        });
        System.out.println("排序后的结果");
        System.out.println("employees02 = " + employees02);
    }
}

class Employee02 {
    private String name;
    private double sal;
    private Mydate02 birthday;

    public Employee02(String name, double sal, Mydate02 birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "\nEmployee02{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public Mydate02 getBirthday() {
        return birthday;
    }

    public void setBirthday(Mydate02 birthday) {
        this.birthday = birthday;
    }
}

class Mydate02 implements Comparable<Mydate02> {
    private int year;
    private int month;
    private int day;


    @Override
    public String toString() {
        return "Mydate02{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    public Mydate02(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public int compareTo(Mydate02 o) {
        //如果名字相同,比较birthday--year
        int yearMinus = year - o.getYear();
        if (yearMinus != 0) {
            return yearMinus;
        }
        //如果year相同,就比较month
        int monthMinus = month - o.getMonth();
        if (monthMinus != 0) {
            return monthMinus;
        }
        //如果year和month都相同,就比较birthday--day
        return day - o.getDay();
    }
}
